package ink.xiaobaibai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ink.xiaobaibai.bo.UserSelectBo;
import ink.xiaobaibai.common.UserIdRoleNameMapping;
import ink.xiaobaibai.dao.AdminUserDao;
import ink.xiaobaibai.entity.AdminUser;
import ink.xiaobaibai.service.IAdminUserRoleService;
import ink.xiaobaibai.service.IAdminUserService;
import ink.xiaobaibai.vo.UserByAdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaobaibai
 * @since 2021-05-11
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserDao, AdminUser> implements IAdminUserService {

    @Autowired
    private IAdminUserRoleService adminUserRoleService;

    @Override
    public Integer getDepId(Integer userId) {
        QueryWrapper<AdminUser> q1 = new QueryWrapper<>();
        q1.select("department_id");
        q1.eq("id", userId);
        return this.getOne(q1).getDepartmentId();
    }

    @Override
    public List<Integer> getUserIdByDepId(Integer depId) {
        QueryWrapper<AdminUser> q1 = new QueryWrapper<>();
        q1.select("id");
        q1.eq("department_id", depId);
        List<AdminUser> list = this.list(q1);
        return list.stream().map(AdminUser::getId).collect(Collectors.toList());
    }

    @Override
    public int getUserVoByAdminCount(UserSelectBo userSelectBo) {
        return this.baseMapper.getUserVoByAdminCount(userSelectBo);
    }

    @Override
    public List<UserByAdminVo> getUserVoByAdminList(UserSelectBo userSelectBo) {
        List<UserByAdminVo> list = this.baseMapper.getUserVoByAdminList(userSelectBo);
        List<Integer> ids = list.stream().map(UserByAdminVo::getUserId).collect(Collectors.toList());
        List<UserIdRoleNameMapping> roleByUserIds = new ArrayList<>();
        if (ids.size() != 0) {
            roleByUserIds = this.adminUserRoleService.getRoleByUserIds(ids);
        }
        Map<Integer, UserByAdminVo> map = new HashMap<>(list.size());
        for (UserByAdminVo userByAdminVo : list) {
            userByAdminVo.setRoleName(new ArrayList<>());
            map.put(userByAdminVo.getUserId(), userByAdminVo);
        }
        for (UserIdRoleNameMapping userIdRoleNameMapping : roleByUserIds) {
            map.get(userIdRoleNameMapping.getUserId()).getRoleName().add(userIdRoleNameMapping.getRoleName());
        }
        return list;
    }

    @Override
    public boolean existsUser(Integer userId) {
        QueryWrapper<AdminUser> q1 = new QueryWrapper<>();
        q1.eq("id", userId);
        q1.last("limit 1");
        return this.count(q1) == 1;
    }

    @Override
    public boolean identicalDep(Integer userId1, Integer userId2) {
        return this.baseMapper.identicalDep(userId1, userId2) == 1;
    }

    @Override
    public List<Integer> getDepUserIdByUserId(Integer userId) {
        return this.baseMapper.getDepUserIdByUserId(userId);
    }

    @Override
    public List<String> getRoleListByUserId(Integer userId) {
        return this.baseMapper.getRoleListByUserId(userId);
    }

    @Override
    public List<AdminUser> getAgentUser() {
        return this.baseMapper.getAgentUser();
    }

    @Override
    public AdminUser getNameAndPhone(Integer id) {
        QueryWrapper<AdminUser> q1 = new QueryWrapper<>();
        q1.select("user_name", "user_phone");
        q1.eq("id", id);
        return this.getOne(q1);
    }

    @Override
    public boolean isSupplierAdminUser(Integer userId) {
        return this.baseMapper.isSupplierAdminUser(userId);
    }

    @Override
    public AdminUser getAgentIdByDepId(Integer depId) {
        return this.baseMapper.getAgentByDepId(depId);
    }

}
